#include "Camera_Debug.h"
#include "DirectInput.h"
#include "Timer.h"
#include "CameraManager.h"

const char* Camera_Debug::NAME = "Camera_Debug";

static bool Registered = TheCameraManager::Instance()->AddCamera(
	Camera_Debug::NAME, 
	new Camera_Debug);


void Camera_Debug::OnActive() 
{
	//TODO: only do this once per camera
	//BuildProjectionMatrix(m_Fov, m_Aspect, m_Near, m_Far);
	//m_Frustum.Init(m_View,m_Proj);

	m_PosX = 0;
	m_PosY = 0;
}

void Camera_Debug::OnDeActive() 
{
}

Camera_Debug::Camera_Debug()
{
	D3DXMatrixIdentity(&m_View);
	D3DXMatrixIdentity(&m_Proj);
	D3DXMatrixIdentity(&m_ViewProj);
	D3DXMatrixIdentity(&m_WorldMatrix);

	m_PosW   = Vec3(0.0f, 0.0f, 0.0f);
	m_RightW = Vec3(1.0f, 0.0f, 0.0f);
	m_UpW    = Vec3(0.0f, 1.0f, 0.0f);
	m_LookW  = Vec3(0.0f, 0.0f, 1.0f);

	//adjust to a value that makes sense for application's.
	m_Speed  = 500.0f;

	//Set a constant rotation on that axis
	m_PosX = 0;
	m_PosY = 0;

	m_Pitch = 0.0f;

	UpdateFrustum = true;
	FrustumDraw = true;

	m_PosW = Vec3(0.0f,5000.0f,-10.0f);
	m_LookW  = Vec3(0.0f, -1.0f, 0.0f);
}

void Camera_Debug::Update()
{
	// Find the net direction the camera is traveling in (since the
	// camera could be running and strafing).
	Vec3 dir(0.0f, 0.0f, 0.0f);
	if( TheInputDevice::Instance()->KeyDown(DIK_W) )
		dir += m_LookW;
	if( TheInputDevice::Instance()->KeyDown(DIK_S) )
		dir -= m_LookW;
	if( TheInputDevice::Instance()->KeyDown(DIK_D) )
		dir += m_RightW;
	if( TheInputDevice::Instance()->KeyDown(DIK_A) )
		dir -= m_RightW;
	
	// Move at m_Speed along net direction.
	D3DXVec3Normalize(&dir, &dir);
	Vec3 newPos = m_PosW + dir * (m_Speed * TheTimer::Instance()->GetDtTime());

	m_PosW = newPos;

	if( TheInputDevice::Instance()->MouseButtonDown(1))
	{
		// We rotate at a fixed speed.
		m_Pitch  = TheInputDevice::Instance()->MouseDY() / 150.0f;
		m_AngleY = TheInputDevice::Instance()->MouseDX() / 150.0f;
	}
	else
	{
		// We rotate at a fixed speed.
		m_Pitch  = m_PosY / 150.0f;
		m_AngleY = m_PosX / 150.0f;
	}

	// Rotate camera's look and up vectors around the camera's right vector.
	Matrix4x4 R;
	D3DXMatrixRotationAxis(&R, &m_RightW, m_Pitch);
	D3DXVec3TransformCoord(&m_LookW, &m_LookW, &R);
	D3DXVec3TransformCoord(&m_UpW, &m_UpW, &R);

	// Rotate camera axes about the world's y-axis.
	D3DXMatrixRotationY(&R, m_AngleY);
	D3DXVec3TransformCoord(&m_RightW, &m_RightW, &R);
	D3DXVec3TransformCoord(&m_UpW, &m_UpW, &R);
	D3DXVec3TransformCoord(&m_LookW, &m_LookW, &R);

	//if( TheInputDevice::Instance()->KeyPressed(DIK_F7) )
	//	UpdateFrustum = !UpdateFrustum;

	//if( TheInputDevice::Instance()->KeyPressed(DIK_F8) )
	//	FrustumDraw = !FrustumDraw;

	//Update the frustum or draw its last updates position
	//if(UpdateFrustum)
	//{
		//m_Frustum.BuildWorldFrustumPlanes(m_View, m_Proj);

	//	if(FrustumDraw)
	//	m_Frustum.BuildFrustumRender();
	//}
	
	// Rebuild the view matrix to reflect changes.
	BuildView();
}

void Camera_Debug::Draw()
{
	//m_Frustum.Render();
}